package com.youyiwen.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.youyiwen.Bean.ChatList;
import com.youyiwen.Bean.Message;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.ChatListMapper;
import com.youyiwen.Mapper.MessageMapper;
import com.youyiwen.Mapper.UserMapper;
import com.youyiwen.Service.ChatListService;
import com.youyiwen.Service.MessageService;
import com.youyiwen.dto.ChatListDTO;
import com.youyiwen.dto.MessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/04/09
 */
@Controller
public class MessageController {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private ChatListMapper chatListMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ChatListService chatListService;

    //
    @GetMapping("/userCenter/communicate")
    public String communicatePage(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        String me = user.getUserName();
        List<ChatListDTO> list = chatListService.list(me);
        model.addAttribute("chatLists", list);
        model.addAttribute("chat", "noChat");
        return "communicate";
    }


    @GetMapping("/userCenter/communicate/{receiver}")
    public String chatWithOthers(@PathVariable("receiver") String receiver,
                                 Model model, HttpSession session) {
        //搜索用户的聊天状态
        User user = (User) session.getAttribute("loginUser");
        String sender = user.getUserName();
        List<ChatListDTO> chatLists = chatListService.list(sender);
        model.addAttribute("chatLists", chatLists);

//        用户不存在
        if (userMapper.selectUserByName(receiver) == null) {
            model.addAttribute("chat", "noUser");
            return "communicate";
        }

        //更改未读消息数的时候，receiver是发送者，而sender是接收者
        messageMapper.updateMessageCount(receiver,sender);

        //将未查看的消息数置零,sender和receiver应该相反
        chatListMapper.updateMsgCount(sender,receiver);
        //查找属于sender的两人会话
        model.addAttribute("receiveUserName", receiver);
        return "communicate";
    }

//    @PostMapping(value = "/communicate/{receiver}")
//    public String sendMassage(@PathVariable("receiver") String receiver,
//                              @RequestParam("chatContent") String content,
//                              HttpSession session) {
//        User user = (User) session.getAttribute("loginUser");
//        String sender = user.getUserName();
//        Long current;
//        //创建两个人的消息
//        for (int i = 1; i <= 2; i++) {
//            Message msg = new Message();
//            current = System.currentTimeMillis();
//            String id = sender + "-" + receiver + "-" + current;
//            msg.setId(id);
//            msg.setSender(sender);
//            msg.setReceiver(receiver);
//            msg.setcha(0);
//            msg.setContent(content);
//            msg.setGmtCreate(current);
//            if (i == 1) {
//                msg.setForUser(sender);
//            } else {
//                msg.setForUser(receiver);
//            }
//            //插入消息
//            messageMapper.insertMsg(msg);
//        }
//
//        if (chatListMapper.selectChatId(sender, receiver) == null)
//        //插入到会话列表
//        {
//            for (int i = 1; i <=2 ; i++) {
//                ChatList chatList = new ChatList();
//                current = System.currentTimeMillis();
//                chatList.setId(sender + current);
//                chatList.setLastReplyTime(current);
//
//                //循环为两个人的对话赋值
//                if (i == 1){
//                    //我是发送者
//                    chatList.setMe(sender);
//                    chatList.setOthers(receiver);
//                }
//                else{
//                    //我是接收者
//                    chatList.setMe(receiver);
//                    chatList.setOthers(sender);
//                }
//
//                chatListMapper.insertChatList(chatList);
//            }
//        }else {
//            current = System.currentTimeMillis();
//            chatListMapper.updateLastReplyTime(sender,receiver,current);
//            chatListMapper.updateMsgCount(receiver,sender);
//        }
//        return "redirect:/userCenter/communicate/{receiver}";
//    }
}
